package cn.com.pajx.pajx_smart_platform.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.just.agentweb.AgentWeb;
import com.just.agentweb.DefaultWebClient;
import com.just.agentweb.WebChromeClient;
import com.just.agentweb.WebViewClient;

import butterknife.BindView;
import cn.com.pajx.pajx_smart_platform.R;
import cn.com.pajx.pajx_smart_platform.api.Api;
import cn.com.pajx.pajx_smart_platform.tools.LogUtil;

/**
 * WebView网页
 */
@SuppressLint("NonConstantResourceId")
public class AgentWebActivity extends BaseActivity {

    @BindView(R.id.fl_content)
    FrameLayout flContent;
    private AgentWeb mAgentWeb;

    private String title;

    @Override
    protected void beforeSetContentView() {
        String url = Api.BASE_URL + Api.LOGIN;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agent_web;
    }

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void initView() {
        //初始化WebView
        initWebView();
    }

    /**
     * 初始化WebView
     */
    private void initWebView() {
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent(flContent, new FrameLayout.LayoutParams(-1, -1))
                .closeIndicator()
                .setWebChromeClient(mWebChromeClient)
                .setWebViewClient(mWebViewClient)
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK)//打开其他应用时，弹窗咨询用户是否前往其他应用
                .interceptUnkownUrl() //拦截找不到相关页面的Scheme
                .createAgentWeb()
                .ready()
                .go("http://ab.zmpajx.com/bd/#/screen_login");
    }

    /**
     * WebChromeClient
     */
    private final WebChromeClient mWebChromeClient = new WebChromeClient() {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }

        @Override
        public void onHideCustomView() {
            super.onHideCustomView();
        }
    };

    //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
    private final WebViewClient mWebViewClient = new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            showProgress();
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            hideProgress();
            super.onPageFinished(view, url);
        }

        @Override
        public void onReceivedError(WebView webView, int i, String s, String s1) {
            hideProgress();
            LogUtil.e("s=" + s);
            LogUtil.e("s1=" + s1);
            super.onReceivedError(webView, i, s, s1);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            hideProgress();
            super.onReceivedError(view, request, error);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //判断用户单击的是那个超连接
            String tag = "tel";
            if (url.contains(tag)) {
                String mobile = url.substring(url.lastIndexOf("/") + 1);
                Uri uri = Uri.parse("tel:" + mobile);
                //电话权限
                Intent intent = new Intent(Intent.ACTION_DIAL, uri);
                startActivity(intent);
                return true;
            }
            view.loadUrl(url);
            return true;
        }
    };

    @Override
    public void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }
}
